package com.tianbao54.grpc.server;

import com.tianbao54.grpc.impl.StudentServiceImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

public class StudentServer {

    private Server server;

    private void start () {

        try {
            server = ServerBuilder.forPort(8888).addService(new StudentServiceImpl()).build().start();
            System.out.println("server started");

            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                System.out.println("shutdown jvm ");
                StudentServer.this.stop();
            }));
            System.out.println(".........");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stop () {

        if (null != server) {
            server.shutdown();
        }
    }

    private void await () throws InterruptedException {

        if (null != server) {
            server.awaitTermination();
        }
    }

    public static void main(String[] args) throws InterruptedException {

        StudentServer server = new StudentServer();
        server.start();
        server.await();
    }
}
